package repostiory

import (
	"context"
	"webook/internal/domain"
	"webook/internal/repostiory/dao"
)

var (
	ErrUserDuplicateEmail = dao.ErrUserDuplicateEmail
	ErrUserNotFind        = dao.ErrUserNotFind
)

type UserRepostiory struct {
	dao *dao.UserDao
}

func NewUserRepostiory(dao *dao.UserDao) *UserRepostiory {
	return &UserRepostiory{
		dao: dao,
	}
}

func (repo *UserRepostiory) Create(ctx context.Context, u domain.User) error {
	return repo.dao.Insert(ctx, dao.User{
		Email:    u.Email,
		Password: u.Password,
	})
}

func (repo *UserRepostiory) FindByEmail(ctx context.Context, email string) (domain.User, error) {
	u, err := repo.dao.FindByEmail(ctx, email)
	if err == dao.ErrUserNotFind {
		return domain.User{}, ErrUserNotFind
	}
	return domain.User{
		Id:       u.Id,
		Email:    u.Email,
		Password: u.Password,
	}, err
}
